package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.ChoiceAddress.Address;
import com.banzhi.shop.entity.Temai.Temai;
import com.banzhi.shop.model.CallBack.AddAddressCallBack;
import com.banzhi.shop.model.IAddAddressModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.AndroidUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 添加地址页面
 */

public class AddAddressImpl implements IAddAddressModel {

    @Override
    public void Save(String id, String realname, String mobile, String province, String city, String area, String address, final AddAddressCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.submitAddressUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        if (id != null) {
            params.addBodyParameter("id", id);
        }
        params.addBodyParameter("realname", realname);
        params.addBodyParameter("mobile", mobile);
        params.addBodyParameter("province", province);
        params.addBodyParameter("city", city);
        params.addBodyParameter("area", area);
        params.addBodyParameter("address", address);
        LogUtils.d("AddAddressImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Temai temai = gson.fromJson(result, Temai.class);
                        callBack.SaveSuccess(temai.getRes());
                    } else if (jsonobject.optString("code").equals("1")) {
                        callBack.SaveError(jsonobject);
                    } /*else if (jsonobject.optString("code").equals("1")) {
                        Gson gson = new Gson();
                        Temai temai = gson.fromJson(result, Temai.class);
                        callBack.addAddess(temai.getRes());
                    }*/
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("AddAddressImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(25)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void getAddress(AddAddressCallBack callBack) {
        String result = AndroidUtil.getJson("area.json", MyApp.context);
        Gson gson = new Gson();
        Address address = gson.fromJson(result, Address.class);
        callBack.getAddress(address);
    }
}
